﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaxiOnline.DAL.DataLayers;
using TaxiOnline.DAL.Models;

namespace TaxiOnline.DAL.Repositories
{
    public enum LoginReturnType {
        Driver, User, InvalidData, InnerError
    }

    public enum RegisterReturnType {  
         Registred, EmailNotUnique, LoginNotUnique, InnerError
    }

    public class UserRepository
    {
        private TaxiContext db = TaxiDbContext.Context;

        public LoginReturnType Login(string login, string password) 
        {
            try
            {
                bool res = false;

                res = db.Users.Any(u => u.Login == login && u.Password == password);

                if (res)
                    return LoginReturnType.User;

                res = db.Drivers.Any(d => d.Login == login && d.Password == password);

                if (res)
                    return LoginReturnType.Driver;

                return LoginReturnType.InvalidData;
            }

            catch (Exception e) {
                return LoginReturnType.InnerError;
            }
        }

        public RegisterReturnType RegisterUser(User user) 
        {
            try
            {
                if (!CheckEmailUnique(user.Email))
                    return RegisterReturnType.EmailNotUnique;

                if (!CheckLoginUnique(user.Login))
                    return RegisterReturnType.LoginNotUnique;

                db.Users.Add(user);
                db.SaveChanges();

                return RegisterReturnType.Registred;
            }
            catch (Exception e) {
                return RegisterReturnType.InnerError;
            }
        }

        public RegisterReturnType RegisterDriver(Driver driver, Vehicle vehicle) {
            try
            {
                if (!CheckEmailUnique(driver.Email))
                    return RegisterReturnType.EmailNotUnique;

                if (!CheckLoginUnique(driver.Login))
                    return RegisterReturnType.LoginNotUnique;

                db.Drivers.Add(driver);
                db.Vehicles.Add(vehicle);
                db.SaveChanges();

                return RegisterReturnType.Registred;
            }
            catch (Exception e)
            {
                return RegisterReturnType.InnerError;
            }
        }

        public List<User> GetAllUsers() {
            List<User> lst = db.Users.ToList();
            return lst;
        }

        public User GetUserById(int id) {
            return db.Users.First(u => u.Id == id);
        }
         
        public IList<Driver> GetAllDrivers() {
            return db.Drivers.ToList(); 
        }

        public Driver GetDriverById(int id) { 
            return db.Drivers.First(u => u.Id == id); 
        }

        private bool CheckLoginUnique(string login) {
            return db.Users.Any(u => u.Login == login) == false 
                && db.Drivers.Any(d => d.Login == login) == false;
        }

        private bool CheckEmailUnique(string email) {
            return db.Users.Any(u => u.Email == email) == false
                    && db.Drivers.Any(d => d.Email == email) == false;
        }
    }
}
